using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.Common;

using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Caching;

using Husb.Data;
using Husb.DataUtil;

using CentralOffice.Enterprise.Data;

using ReceiverTable = CentralOffice.Enterprise.Data.NoticeReceiverDataSet.NoticeReceiverDataTable;
using ReceiverRow = CentralOffice.Enterprise.Data.NoticeReceiverDataSet.NoticeReceiverRow;


namespace CentralOffice.Enterprise.DataAccess
{
    public class NoticeReceiverAdapter : DataAccessAdapter<NoticeReceiverDataSet, ReceiverTable, ReceiverRow>
    {
        public const string FetchByNoticeIdStoredProc = "Enterprise.NoticeReceiver_SelectNoticeReceiverByNoticeId";
        public const string FetchByEnterpriseIdStoredProc = "Enterprise.NoticeReceiver_SelectNoticeReceiverByEnterpriseId";
        public const string ReplyNoticeStoredProc = "Enterprise.NoticeReceiver_ReplyNotice";
        public const string FetchNoticeCountByEnterpriseIdStoredProc = "Enterprise.NoticeReceiver_SelectNoticeCountByEnterpriseId";
        public const string FetchNoticeCountByENIdStoredProc = "Enterprise.NoticeReceiver_SelectNoticeReceiverByENId";

        public NoticeReceiverAdapter()
        {
            this.TableName = "NoticeReceiver";

            GetAllStoredProc = "Enterprise.NoticeReceiver_SelectNoticeReceiversAll";
            InsertStoredProc = "Enterprise.NoticeReceiver_InsertNoticeReceiver";
            UpdateStoredProc = "Enterprise.NoticeReceiver_UpdateNoticeReceiver";
            DeleteStoredProc = "Enterprise.NoticeReceiver_DeleteNoticeReceiver";

            GetByIdStoredProc = "Enterprise.NoticeReceiver_SelectNoticeReceiver";
            GetByNameStoredProc = "";
            GetPagedStoredProc = "Enterprise.NoticeReceiver_SelectNoticeReceiversPaged";
            GetRowCountStoredProc = "Enterprise.NoticeReceiver_SelectNoticeReceiversRowCount";

            FindStoredProc = "Enterprise.NoticeReceiver_SelectNoticeReceiversDynamic";
        }

        protected override void PopulateInsertParameters(Database db, DbCommand cmd)
        {
            //throw new Exception("The method or operation is not implemented.");
            db.AddInParameter(cmd, "Id", DbType.Guid, "Id", DataRowVersion.Current);
            db.AddInParameter(cmd, "NoticeId", DbType.Guid, "NoticeId", DataRowVersion.Current);
            db.AddInParameter(cmd, "EnterpriseId", DbType.Guid, "EnterpriseId", DataRowVersion.Current);
            db.AddInParameter(cmd, "ApprovedBy", DbType.String, "ApprovedBy", DataRowVersion.Current);
            db.AddInParameter(cmd, "ApprovedDate", DbType.DateTime, "ApprovedDate", DataRowVersion.Current);
            db.AddInParameter(cmd, "ApprovedText", DbType.String, "ApprovedText", DataRowVersion.Current);
            db.AddInParameter(cmd, "IsApproved", DbType.Boolean, "IsApproved", DataRowVersion.Current);
        }

        public ReceiverTable FetchByNoticeId(Guid id, bool isApproved)
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();
            parameters.Add(new DatabaseParameter("NoticeId", DbType.Guid, id));
            parameters.Add(new DatabaseParameter("IsApproved", DbType.Boolean, isApproved));

            return this.GetTable(FetchByNoticeIdStoredProc, parameters, true, false, null);
        }

        public ReceiverTable FetchByEnterpriseId(Guid id, bool isApproved)
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();
            parameters.Add(new DatabaseParameter("EnterpriseId", DbType.Guid, id));
            parameters.Add(new DatabaseParameter("IsApproved", DbType.Boolean, isApproved));

            //return this.GetTable(FetchByEnterpriseIdStoredProc,  parameters, true, false, null);

            return this.GetTable(FetchByEnterpriseIdStoredProc, parameters, true, false, null);
        }

        public int ReplyNotice(Guid id, string approvedBy, DateTime approvedDate, string approvedText, bool isApproved)
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();

            parameters.Add(new DatabaseParameter("Id", DbType.Guid, id));
            parameters.Add(new DatabaseParameter("ApprovedBy", DbType.String, approvedBy));
            parameters.Add(new DatabaseParameter("ApprovedDate", DbType.DateTime, approvedDate));
            parameters.Add(new DatabaseParameter("ApprovedText", DbType.String, approvedText));
            parameters.Add(new DatabaseParameter("IsApproved", DbType.Boolean, isApproved));

            return this.ExecuteNonQuery(ReplyNoticeStoredProc, parameters, true, false);
        }


        public int FetchNoticeCountByEnterpriseId(Guid enterpriseId, bool isApproved)
        {
            //List<DatabaseParameter> parameters = new List<DatabaseParameter>();

            //parameters.Add(new DatabaseParameter("EnterpriseId", DbType.Guid, enterpriseId));
            //parameters.Add(new DatabaseParameter("IsApproved", DbType.Boolean, isApproved));

            //object[] objects = new object[] { enterpriseId, isApproved };
            object o = this.GetValue(FetchNoticeCountByEnterpriseIdStoredProc, new PopulateCustomParameters(PopulateEnterpriseIdParameters), true, false, null, enterpriseId, isApproved);

            if (o != null)
            {
                return (int)o;
            }

            return 0;
            //throw new Exception("The method or operation is not implemented.");
        }

        private void PopulateEnterpriseIdParameters(Database db, DbCommand cmd, object[] objects)
        {
            //  Guid enterpriseId, bool isApproved
            db.AddInParameter(cmd, "EnterpriseId", DbType.Guid, objects[0]);
            db.AddInParameter(cmd, "IsApproved", DbType.Boolean, objects[1]);
        }

        public ReceiverRow FetchById(Guid enterpriseId, Guid noticeId )
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();
            parameters.Add(new DatabaseParameter("NoticeId", DbType.Guid, noticeId));
            parameters.Add(new DatabaseParameter("EnterpriseId", DbType.Guid, enterpriseId));
            
            //return this.GetTable(FetchByEnterpriseIdStoredProc,  parameters, true, false, null);

            ReceiverTable table = this.GetTable(FetchNoticeCountByENIdStoredProc, parameters, true, false, null);
            if (table != null && table.Rows.Count > 0)
            {
                return table.Rows[0] as ReceiverRow;
            }
            return null;
        }


    }
}
